I am trying to make a navbar using react.js but i got this error:
Error: Invalid hook call. Hooks can only be called inside of the body of a function component. This could happen for one of the following reasons:
- You might have mismatching versions of React and the renderer (such as React DOM)
- You might be breaking the Rules of Hooks
- You might have more than one copy of React in the same app See https://reactjs.org/link/invalid-hook-call for tips about how to debug and fix this problem.
This is my code in index.js
import { Nav, NavLink, NavMenu } from './NavbarElements'
const Navbar = () => {
return (
<>
<Nav>
<NavLink to='/'>
<h1>Logo</h1>
</NavLink>
<NavMenu>
<NavLink to='/home' activeStyle>
Home
</NavLink>
<NavLink to='/about' activeStyle>
About
</NavLink>
</NavMenu>
</Nav>
</>
);
};
export default Navbar;
and this is my code in NavbarElements.js
import { NavLink as Link } from 'react-router-dom';
export const Nav = styled.nav`
background: #000;
height: 80px;
display: flex;
justify-content: space-between;
padding: 0.5rem calc((100wv - 1000px) /2);
z-index: 10;
`;
export const NavLink = styled(Link)`
color: #fff;
display: flex;
align-items: center;
text-decoration: none;
padding: 0 lrem;
height: 100%;
cursor: pointer;
&.active{
color: #15cdfc;
}
`;
export const NavMenu = styled.div`
display: flex;
align-items: center;
margin-right: 24px;
@media screen and (max-width:768px) {
display: none;
}
`;
I am a really new programmer so I apologise in advance if I am making a stupid mistake. Thanks!
Try to add import React from “react”, at the first line, as you are not importing react you cant make use of hooks